﻿using Net5ApiAndEfMySql.DtoModel.Common;
using Net5ApiAndEfMySql.DtoModel.SystemAdmin;
using Net5ApiAndEfMySql.IRepository.SystemAdmin;
using Net5ApiAndEfMySql.PoModel.SystemAdmin;
using Net5CommonLib.SystemLib.TypeTools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Net5ApiAndEfMySql.DtoModel;
using VueAndNetCoreEfApi.PoModel.SystemAdmin;
using Microsoft.EntityFrameworkCore;

namespace Net5ApiAndEfMySql.Repository.SystemAdmin
{
    public class OperatorRepository : BaseRepository, IOperatorRepository
    {
        private IRoleDbRepository _iRoleDbRepository;
        public OperatorRepository(IRoleDbRepository iRoleDbRepository)
        {
            _iRoleDbRepository = iRoleDbRepository;
        }

        //通过用户名获取登录用户
        public OperatorPo GetAdminUserByUserName(string userName)
        {
            return SoaSystemCenterDbContext.OperatorPo.FirstOrDefault(c => c.op_account == userName);
        }
        public OperatorDto GetAdminUserDtoByUserName(string userName)
        {
            OperatorPo userPo = SoaSystemCenterDbContext.OperatorPo.FirstOrDefault(c => c.op_account == userName);
            OperatorDto userDto = new OperatorDto();
            ModelCopier.CopyModel(userPo, userDto);
            userDto.RoleList = _iRoleDbRepository.GetRoleByOprole(userName);
            return userDto;
        }

        //操作员显示
        public PagerResult<OperatorDto> GetPageList(SearchModel m)
        {
            //获取Queryable
            IQueryable<OperatorPo> opIq = SoaSystemCenterDbContext.OperatorPo.AsQueryable();
            IQueryable<RoleDbPo> roleIq = SoaSystemCenterDbContext.RoleDbPo.AsQueryable();
            IQueryable<EmployeePo> empIq = SoaSystemCenterDbContext.EmployeePo.AsQueryable();
            //linq拼接Queryable
            var qb = from a in opIq 
                     join b in roleIq on a.op_role equals b.role_id 
                     join c in empIq on a.op_id equals c.emp_id
                     select new OperatorDto 
                     { 
                         op_account=a.op_account,
                         op_card=a.op_card,
                         op_id=a.op_id,
                         op_createtime=a.op_createtime,
                         op_phone=a.op_phone,
                         op_position_name=a.op_position_name,
                         op_pwd=a.op_pwd,
                         op_role=a.op_role,
                         op_state=a.op_state,
                         RoleName=b.role_name,
                         departmentId=c.emp_dp_id,
                         userName=c.emp_name
                     };
                    
            if (m.SearchItem != null && m.SearchItem.Count > 0)
            {
                var tempDi = m.SearchItem;
                string op_card = "op_card";
                if (tempDi.Keys.Contains(op_card) && !string.IsNullOrEmpty(tempDi[op_card]))
                {
                    qb = qb.Where(c => c.op_card.Contains(tempDi[op_card]));
                }
                string RoleName = "RoleName";
                if (tempDi.Keys.Contains(RoleName) && !string.IsNullOrEmpty(tempDi[RoleName]))
                {
                    qb = qb.Where(c => c.RoleName.Contains(tempDi[RoleName]));
                }
                string op_phone = "op_phone";
                if (tempDi.Keys.Contains(op_phone) && !string.IsNullOrEmpty(tempDi[op_phone]))
                {
                    qb = qb.Where(c => c.op_phone.Contains(tempDi[op_phone]));
                }
                string departmentId = "departmentId";
                if (tempDi.Keys.Contains(departmentId) && !string.IsNullOrEmpty(tempDi[departmentId]))
                {
                    qb = qb.Where(c => c.departmentId == int.Parse(tempDi[departmentId]));
                }
            }
            return Pager(qb, m);
        }
        //操作员添加
        public bool AddOperator(OperatorDto obj)
        {
            if (obj == null) return false;
            try
            {
                //添加用户
                SoaSystemCenterDbContext.Add(obj);
                //保存
                SoaSystemCenterDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        //操作员修改
        public bool UptOperator(OperatorDto obj)
        {
            if (obj == null) return false;
            try
            {
                var opPo = SoaSystemCenterDbContext.OperatorPo.FirstOrDefault(p => p.op_account == obj.op_account);
                ModelCopier.CopyModel(obj, opPo, "op_card", "op_createtime", "op_account", "op_pwd");
                SoaSystemCenterDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        //操作员状态修改
        public bool UptStateByCard(string card)
        {
            if (card == null) return false;
            try
            {
                var opPo = SoaSystemCenterDbContext.OperatorPo.FirstOrDefault(p => p.op_card == card);
                if (opPo.op_state == 1)
                {
                    opPo.op_state = 0;
                }
                else
                {
                    opPo.op_state = 1;
                }
                SoaSystemCenterDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        //操作员删除
        public bool DelOperator(string card)
        {
            if (card == null) return false;
            try
            {
                string sql = "delete from operator where op_card=@p0";
                SoaSystemCenterDbContext.Database.ExecuteSqlRaw(sql, card);
                SoaSystemCenterDbContext.SaveChanges();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        //操作员批量删除
        public bool DelOperatorList(string keys)
        {
            if (keys == null) return false;
            //事务
            using (var tran = SoaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    var arr = keys.Split(',');
                    foreach (var item in arr)
                    {
                        string sql = "delete from operator where op_card=@p0";
                        SoaSystemCenterDbContext.Database.ExecuteSqlRaw(sql, item);
                    }
                    //保存
                    SoaSystemCenterDbContext.SaveChanges();
                    //提交事务
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                    return false;
                }
            }
                return true;
        }
        //操作员获取/唯一验证
        public OperatorPo GetOperatorByCard(string card)
        {
            return SoaSystemCenterDbContext.OperatorPo.FirstOrDefault(c => c.op_card == card);
        }
        public OperatorPo GetOperatorByAccount(string account)
        {
            return SoaSystemCenterDbContext.OperatorPo.FirstOrDefault(c => c.op_account == account);
        }

        //获取角色
        public List<RoleDbPo> GetRoles()
        {
            return SoaSystemCenterDbContext.RoleDbPo.ToList();
        }
        //获取员工
        public List<EmployeePo> GetEmployees()
        {
            return SoaSystemCenterDbContext.EmployeePo.ToList();
        }
    }
}
